// mircoApp/kaipin_laboratory/index/index.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    img_url: "",
    comment_list_details: [],
    fixed_top: 0,
    my_product_count: 0,
    my_comment_count: 0,

    comment_status: 3,
    sign_up_flag: false,
    earnest_money_flag: false,
    active: 0,
    keyword: "",
    list: [],
    isLoadMore: true,
    page: 1,
    total_page: 0,
    select_flag: false,
    open_status: null,
    earnest_money_status: null,
    sign_up_status: null,
    sign_txt: "报名状态",
    open_text: "开品状态",
    open_list: [{
        txt: "全部",
        status: 99
      },
      {
        txt: "待出品",
        status: 0
      },
      {
        txt: "已出品",
        status: 1
      },
      {
        txt: "出品中",
        status: 2
      },
      {
        txt: "已完成",
        status: 3
      },
      {
        txt: "开品失败",
        status: -1
      },
    ],
    earnest_money_list: [{
        txt: "全部",
        status: 99
      }, {
        txt: "待缴纳",
        status: 0
      },
      {
        txt: "已缴纳",
        status: 1
      },
      {
        txt: "已退还",
        status: -1
      },

    ],
    sign_up_list: [{
        txt: "全部",
        status: 99
      }, {
        txt: "待通过",
        status: 0
      },
      {
        txt: "已通过",
        status: 1
      },
      {
        txt: "已取消",
        status: -1
      },

    ],

    earnest_money_txt: "保证金状态",
    inner_width: 0,
    comment_list: [{
        txt: '全部',
        status: 3
      },
      {
        txt: '通过',
        status: 1
      },
      {
        txt: '未通过',
        status: 0
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onChange(event) {
    let active = event.detail.index;
    this.setData({
      active
    });
    if (active) {
      this.get_comment_list();
    } else {
      this.getData()
    }
  },

  change(e) {
    let type = e.currentTarget.dataset.type;
    let data = e.currentTarget.dataset.item;
    console.log(data, 'data', '==========');
    let txt = 'sign_txt',
      status = 'sign_up_status',
      flag = 'sign_up_flag';
    if (type == 'money') {
      txt = 'earnest_money_txt';
      status = 'earnest_money_status';
      flag = 'earnest_money_flag';
    } else if (type == 'select') {
      txt = 'open_text';
      status = 'open_status';
      flag = 'select_flag';
    } else if (type == 'comment') {
      status = 'comment_status';
    }

    this.setData({
      [txt]: data.txt,
      [status]: data.status,
      [flag]: false
    })
    if (type == 'comment') {
      this.get_comment_list();
    } else {
      this.getData();
    }
  },
  get_comment_list(page = 1) {
    let json = {
      page
    }
    if (this.data.comment_status !== 3) {
      json.is_public = this.data.comment_status;
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.kaipin-laboratory.frontend.product-plaza.get-my-comment"),
      data: json,
      success: resdata => {
        let {
          result,
          data
        } = resdata.data;
        if (result) {
          console.log(data);
          let comment_list_details = page == 1 ? data.data : this.data.comment_list_details.concat(data.data);
          this.setData({
            comment_list_details
          });
          this.data.page = data.current_page;
          this.data.total_page = data.last_page;
        }
      }
    })
  },
  select_handle(e) {
    let type = e.currentTarget.dataset.type;
    console.log('父元素');
    let data_var = 'sign_up_flag'
    if (type == 'select') {
      data_var = 'select_flag';
      this.setData({
        sign_up_flag: false,
        earnest_money_flag: false
      })
    } else if (type == 'earnest_money') {
      data_var = 'earnest_money_flag'
      this.setData({
        sign_up_flag: false,
        select_flag: false
      })
    } else {
      this.setData({
        earnest_money_flag: false,
        select_flag: false
      })
    }
    this.setData({
      [data_var]: !this.data[data_var]
    })
  },
  goto(e) {
    let url = e.currentTarget.dataset.url;
    if (url == 'index') {
      wx.navigateBack();
      return;
    }
    let item = e.currentTarget.dataset.item;
    let id = "";
    if (url == 'publish_comment') {
      id = item.product_id
    } else {
      id = item.id

    }
    wx.navigateTo({
      url: `/mircoApp/kaipin_laboratory/${url}/${url}?id=${id}`,
    })
  },
  getData(page = 1) {
    let json = {
      page,
      product_status: this.data.open_status == 99 ? '' : this.data.open_status,
      margin_status: this.data.earnest_money_status == 99 ? '' : this.data.earnest_money_status,
      status: this.data.sign_up_status == 99 ? '' : this.data.sign_up_status,
      product_name: this.data.keyword
    }
    app._postNetWork({
      url: app.getNetAddresss('plugin.kaipin-laboratory.frontend.product-plaza.get-product-log'),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          this.data.page = data.current_page;
          this.data.total_page = data.last_page;
          let list = page == 1 ? data.data : this.list.concat(data.data);
          this.setData({
            my_product_count: data.my_product_count,
            my_comment_count: data.my_comment_count,
            list,
            member: data.member,

          })
        } else {
          app.tips(msg);
        }
        let yz_basic_inf = wx.getStorageSync('yz_basic_info');
        let img_url = yz_basic_inf.host + 'kaipin-laboratory/empty.png';
        this.setData({
          img_url
        })
      }
    })
  },
  getMoreData() {
    if (this.data.page >= this.data.total_page) return;
    let page = this.data.page + 1;
    if (this.data.active == 0) {
      this.getData(page);
    } else {
      this.get_comment_list(page);
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.getData();
    if (this.data.active == 0) {
      let query = wx.createSelectorQuery();
      query.select('.select-float-hidden').boundingClientRect(rect => {
        console.log("fixed_topppp", rect);
        console.log(rect);
        this.setData({
          fixed_top: rect.top + 'px'
        });
      }).exec();
    }

  },
  onSearch() {
    this.getData();
  },
  onSearchChange(e) {
    this.setData({
      keyword: e.detail,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})